import time

import pytest
from selenium.webdriver.common.by import By
from selenium.webdriver.support.wait import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from Auto.day02.login.Login_Operation import Login_Operation
import re

class Loan_Operation(Login_Operation):
    pre_money = 0.0


    @pytest.fixture(scope="function")
    def daikuan(self,login_driver,login_page,username,password,money,days):
        login_driver.get('http://localhost:90/user/tools/toApplyLoan.html')
        # 借贷金额//*[@id="amount"]
        login_driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(money)
        # 借贷天数
        login_driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(days)
        # 申请
        login_driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        # 确认
        # chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        # wait = WebDriverWait(login_driver, 1)
        # wait.until(
        #     EC.element_to_be_clickable(
        #         (By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]'))).click()
        time.sleep(1)
        login_driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(0.5)

    @pytest.fixture(scope="function")
    def daikuan_and_shenpi(self,login_driver,username,password,money,days,admin,adpassword,login_page):
        login_driver.get('http://localhost:90/user/tools/toApplyLoan.html')
        # 借贷金额//*[@id="amount"]
        login_driver.find_element(By.XPATH, '//*[@id="amount"]').send_keys(money)
        # 借贷天数
        login_driver.find_element(By.XPATH, '//*[@id="term"]').send_keys(days)
        # 申请
        login_driver.find_element(By.XPATH, '//*[@id="submit"]').click()
        # 确认
        # chromeDriver.find_element(By.XPATH,'/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        # wait = WebDriverWait(login_driver, 2)
        # wait.until(
        #     EC.element_to_be_clickable(
        #         (By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]'))).click()
        time.sleep(1)

        login_driver.find_element(By.XPATH, "//div[@class='jconfirm-buttons']//button[@class='btn btn-primary']").click()

        time.sleep(1)
        self.pre_money = self.getMoney(login_driver)
        print("当前余额：" + str(money))
        print("申请成功")
        time.sleep(1)
        # 登录到后台
        self.switch_login(login_driver,admin,adpassword)

        #跳转页面
        login_driver.get('http://localhost:90/admin/loan/toLoanexam.html')
        #跳转到末页  ：  /html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul/li[6]/a
        login_driver.find_element(By.XPATH,'/html/body/div/div/main/div/div/div/div/div[3]/div[2]/nav/ul/li[6]/a').click()
        time.sleep(1)
        # 审核通过按钮XPATH  /html/body/div[2]/div[2]/div/div/div[2]/table/tbody/tr[1]/td[5]/button
        # 获取最后一个审核通过按钮，并点击
        login_driver.find_elements(By.CSS_SELECTOR, 'button.btn-primary.btn-sm.pass_update_btn')[-1].click()
        time.sleep(1)
        # 确认按钮XPATH  /html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]
        login_driver.find_element(By.XPATH,
                                  '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[4]/button[1]').click()
        time.sleep(2)
        print("审核成功")
        self.switch_login(login_driver,username,password)





    def getMoney(self,login_driver):
        login_driver.get('http://localhost:90/user/personal/toBankCard.html')
        # 使用XPATH可以定位"//*[contains(text(), '默认')]"带有特定文字的元素
        h4 = login_driver.find_element(By.XPATH, "//*[contains(text(), '默认')]")
        ## 定位上一级的上一级元素（祖父级元素）
        # 示例： grandparent_element = div_element.find_element(By.XPATH, "../..")
        grandparent_element = h4.find_element(By.XPATH, "../..")
        # 定位祖父的兄弟元素
        ## 定位同级元素
        # 示例：sibling_element = div_element.find_element(By.XPATH, "following-sibling::div")
        sibling_element = grandparent_element.find_element(By.XPATH, "following-sibling::div")
        # 定位兄弟元素的第一个子元素
        # ch1 = sibling_element.find_element(By.XPATH, "./h4[1]")
        # 定位兄弟元素的第二个子元素
        ch2 = sibling_element.find_element(By.XPATH, "./h4[2]")
        # cardnum1 = ch1.text
        # print(cardnum1)
        money1 = ch2.text
        # 使用正则表达式提取数字部分
        balance_match = re.search(r"\d+\.\d+", money1)

        if balance_match:
            # 将提取到的字符串转换为浮点数
            money1 = float(balance_match.group())

        return money1
    def assert_money(self,login_driver,money,result,pre_money):
        if result == money + pre_money:
            print("通过")
        else:
            print("不通过")
            import os
            if not os.path.exists("demo"):
                os.mkdir("demo")
            login_driver.save_screenshot("demo/贷款.png")
        assert result ==  money + pre_money
    def getResultMSG(self,login_driver):
        #/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div
        return login_driver.find_element(By.XPATH, '/html/body/div[2]/div[2]/div/div/div/div/div/div/div/div[3]/div/div').text

